package com.fzk.blog.blog.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fzk.blog.bean.Blog;
import com.fzk.blog.blog.service.IRedisService;
import com.fzk.blog.blog.service.ITimeLineService;
import com.fzk.blog.utils.MyJsonUtil;
import com.fzk.blog.utils.ResponseResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @author fzk
 * @date 2021-08-23 20:42
 */
@RestController
public class TimeLineController {
    @Resource
    private ITimeLineService timeLineService;
    @Resource
    private IRedisService redisService;

    @GetMapping(path = "/blog/timeLine")
    public Map<String, Object> timeLine() throws JsonProcessingException {
        // 1.从redis查
        String json = redisService.get("blog_timeLine");
        // 2.没有则调service，并放入redis
        if (json != null)
            return ResponseResult.success("查询成功", json);

        List<List<List<Blog>>> blog_time_line = null;
        blog_time_line = timeLineService.getBlogTimeLine();
        json = MyJsonUtil.writeToString(blog_time_line);
        redisService.setEx("blog_timeLine", json);
        return ResponseResult.success("查询成功", json);
    }
}
